﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DemoMenu_01.Models.Interface;
using System.Web.Security;
using IEntity.Entity;
using DemoMenu_01.Models.Service;

namespace DemoMenu_01.Models.Exec
{
    public class ExecAccount : IAccount
    {
        #region provider
        private MembershipProvider _provider;
        private MembershipProvider provider
        {
            get
            {
                if (_provider != null)
                {
                    return _provider;
                }
                else
                {
                    _provider = Membership.Provider;
                    return _provider;
                }
            }
            
        }
        private EntitiyExtent _unProEntity;
        private EntitiyExtent unProEntity
        {
            get
            {
                if (_unProEntity != null)
                {
                    return _unProEntity;
                }
                else
                {
                    _unProEntity = new EntitiyExtent();
                    
                    return _unProEntity;
                }
            }

        }
        EntitiyExtent Entity = new EntitiyExtent();

        #endregion
        public void SignIn(string userName, bool createPersistentCookie)
        {
            if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Value cannot be null or empty.", "userName");

            FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
        }
        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }
        public MembershipCreateStatus Register(string Username, string Password)
        {
            return Register(Username, Password, string.Empty);
        }
        public MembershipCreateStatus Register(string Username, string Password, string Email)
        {
            if (String.IsNullOrEmpty(Username)) throw new ArgumentException("Value cannot be null or empty.", "userName");
            if (String.IsNullOrEmpty(Password)) throw new ArgumentException("Value cannot be null or empty.", "password");
            //if (String.IsNullOrEmpty(Email)) throw new ArgumentException("Value cannot be null or empty.", "email");
            Email = "justintriet112@yahoo.com";
            MembershipCreateStatus status;
            provider.CreateUser(Username, Password, Email, null, null, true, null, out status);

            Per_UserInfo userInfo = new Per_UserInfo(){
                ID=Guid.NewGuid(),
                Email= Email
            };
            unProEntity.addEntity<Per_UserInfo>(new List<Per_UserInfo>() { userInfo });
            unProEntity.SaveChanges();  
            return status;
        }
        public bool ChangePassword(string Username, string OldPassword, string NewPassWord)
        {
            if (String.IsNullOrEmpty(Username)) throw new ArgumentException("Value cannot be null or empty.", "userName");
            if (String.IsNullOrEmpty(OldPassword)) throw new ArgumentException("Value cannot be null or empty.", "oldPassword");
            if (String.IsNullOrEmpty(NewPassWord)) throw new ArgumentException("Value cannot be null or empty.", "newPassword");

            // The underlying ChangePassword() will throw an exception rather
            // than return false in certain failure scenarios.
            try
            {
                MembershipUser currentUser = _provider.GetUser(Username, true /* userIsOnline */);
                return currentUser.ChangePassword(OldPassword, NewPassWord);
            }
            catch (ArgumentException)
            {
                return false;
            }
            catch (MembershipPasswordException)
            {
                return false;
            }
        }
    }
}